package cn.com.ejiakanghu.web;

import cn.com.ejiakanghu.constant.ScanGunConstant;
import cn.com.ejiakanghu.core.exception.CheckException;
import cn.com.ejiakanghu.core.utils.RespUtil;
import cn.com.ejiakanghu.core.vo.BaseResult;
import cn.com.ejiakanghu.form.*;
import cn.com.ejiakanghu.service.AnnouncerManager;
import cn.com.ejiakanghu.service.ScanGunManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("scanGun")
public class ScanGunController {

    private Logger logger = LoggerFactory.getLogger(ScanGunController.class);

    @Autowired
    private ScanGunManager scanGunManager;

    /**
     * 扫码枪绑定门店
     * @return
     */
    @RequestMapping("/bindStore")
    public BaseResult bindStore( @RequestParam("storeId") String storeId, HttpServletRequest request) {
        String deviceId=request.getHeader(ScanGunConstant.DEVICE_ID_HEADER);
        return scanGunManager.bindStore(deviceId, storeId);
    }

    /**
     * 扫码枪解绑门店
     * @return
     */
    @RequestMapping("/unBindStore")
    public BaseResult unBindStore(HttpServletRequest request) {
        String deviceId=request.getHeader(ScanGunConstant.DEVICE_ID_HEADER);
        return scanGunManager.unBindStore(deviceId);
    }

    /**
     * 医柜绑定门店
     * @return
     */
    @RequestMapping("/bindLocker")
    public BaseResult bindLocker(
                                 @RequestParam("storeId") String storeId,
                                 @RequestParam("lockerId") String lockerId,
                                 HttpServletRequest request) {
        String deviceId=request.getHeader(ScanGunConstant.DEVICE_ID_HEADER);
        return scanGunManager.bindLocker(storeId, lockerId);
    }

    /**
     * 医柜解绑门店
     * @return
     */
    @RequestMapping("/unBindLocker")
    public BaseResult unBindLocker(@RequestParam(name = "storeId", required = false) String storeId,
                                   @RequestParam("lockerId") String lockerId,
                                   HttpServletRequest request) {
        String deviceId=request.getHeader(ScanGunConstant.DEVICE_ID_HEADER);
        return scanGunManager.unBindLocker(lockerId);
    }

    /**
     * 获取扫码枪绑定信息
     * @return
     */
    @RequestMapping("/bindInfo")
    public BaseResult bindInfo(HttpServletRequest request) {
        String deviceId=request.getHeader(ScanGunConstant.DEVICE_ID_HEADER);
        return scanGunManager.bindInfo(deviceId);
    }

    /**
     * 获取指定RFID列表对应的商品列表
     * @return
     */
    @RequestMapping("/storeGoodsByRfidList")
    public BaseResult storeGoodsByRfidList(@RequestBody StoreGoodsByRfidListForm storeGoodsByRfidListForm) {
        return scanGunManager.storeGoodsByRfidList(storeGoodsByRfidListForm);
    }

    /**
     * 更新门店当前商品库存列表
     * @return
     */
    @RequestMapping("/updateStoreGoods")
    public BaseResult updateStoreGoods(@RequestBody ScanGunUpdateStoreGoodsListForm updateStoreGoodsListForm) {
        return scanGunManager.updateStoreGoods(updateStoreGoodsListForm);
    }

    /**
     * 编辑门店商品状态
     * @return
     */
    @RequestMapping("/editStoreGoodsStatus")
    public BaseResult editStoreGoodsStatus(
                                           @RequestParam("rfid") String rfid,
                                           @RequestParam("status") Integer status,
                                           HttpServletRequest request) {
        String deviceId=request.getHeader(ScanGunConstant.DEVICE_ID_HEADER);
        return scanGunManager.editStoreGoodsStatus(deviceId, rfid, status);
    }

    /**
     * 搜索指定RFID的门店商品
     * @return
     */
    @RequestMapping("/searchStoreGoods")
    public BaseResult searchStoreGoods(
                                           @RequestParam("rfid") String rfid,
                                           HttpServletRequest request) {
        String deviceId=request.getHeader(ScanGunConstant.DEVICE_ID_HEADER);
        return scanGunManager.searchStoreGoods(rfid);
    }

    /**
     * 获取门店当前商品库存列表
     * @return
     */
    @RequestMapping("/storeGoods")
    public BaseResult storeGoods(HttpServletRequest request) {
        String deviceId=request.getHeader(ScanGunConstant.DEVICE_ID_HEADER);
        return scanGunManager.storeGoods(deviceId);
    }

    /**
     * 更换门店商品RFID
     * @return
     */
    @RequestMapping("/editStoreGoodsRfid")
    public BaseResult editStoreGoodsRfid(
                                 @RequestParam("oldRfid") String oldRfid,
                                 @RequestParam("newRfid") String newRfid,
                                 HttpServletRequest request) {
        String deviceId=request.getHeader(ScanGunConstant.DEVICE_ID_HEADER);
        return scanGunManager.editStoreGoodsRfid(deviceId, oldRfid, newRfid);
    }


    /**
     * 入库/归还门店商品
     */
    @RequestMapping("/inStockStoreGoods")
    public BaseResult inStockStoreGoods(@RequestBody InStockStoreGoodsForm inStockStoreGoodsForm,HttpServletRequest request){
        String deviceId=request.getHeader(ScanGunConstant.DEVICE_ID_HEADER);
        return scanGunManager.inStockStoreGoods(inStockStoreGoodsForm,deviceId);
    }

    /**
     * 获取订单医柜配货清单列表
     */
    @RequestMapping("/orderPickupList")
    public BaseResult orderPickupList( @Validated OrderPickupListForm orderPickupListForm, HttpServletRequest request){
        String deviceId=request.getHeader(ScanGunConstant.DEVICE_ID_HEADER);
        orderPickupListForm.setDeviceId(deviceId);
        return scanGunManager.orderPickupList(orderPickupListForm);
    }

    /**
     * 订单配货完成
     * @return
     */
    @RequestMapping("/finishOrderPickup")
    public BaseResult finishOrderPickup(@RequestBody @Validated FinishOrderPickupForm finishOrderPickupForm){
        BaseResult result = null;
        try {
            result = scanGunManager.finishOrderPickup(finishOrderPickupForm);
        } catch (CheckException e) {
            logger.error("订单完成配货异常:{}",e.getResultCode());
            return RespUtil.fail(e.getResultCode());
        }
        return result;
    }

    /**
     * 医柜线下异常出库完成
     */
    @RequestMapping("/finishOfflinePickup")
    public BaseResult finishOfflinePickup(@RequestBody @Validated FinishOfflinePickupForm finishOfflinePickupForm){
        return scanGunManager.finishOfflinePickup(finishOfflinePickupForm);
    }

    /**
     *  获取门店可录入的商品列表
     */
    @RequestMapping("/getStoreGoodsList")
    public BaseResult getStoreGoodsList( @Validated StoreGoodsListForm form,HttpServletRequest request){
        String deviceId=request.getHeader(ScanGunConstant.DEVICE_ID_HEADER);
        form.setDeviceId(deviceId);
        return scanGunManager.getStoreGoodsList(form);
    }

    /**
     * 门店商品录入
     */
    @RequestMapping("/storeAddGoods")
    public BaseResult storeAddGoods(@RequestBody @Validated StoreAddGoodsForm storeAddGoodsForm,HttpServletRequest request){
        String deviceId=request.getHeader(ScanGunConstant.DEVICE_ID_HEADER);
        storeAddGoodsForm.setDeviceId(deviceId);
        return scanGunManager.storeAddGoods(storeAddGoodsForm);
    }

    //------------测试使用  --------------------

    /**
     * 配送完成
     * @param missionId
     * @return
     */
    @RequestMapping("/orderOver")
    public BaseResult orderOver(Integer missionId){
        return scanGunManager.orderOver(missionId);
    }

    /**
     * 创建任务
     * @return
     */
    @RequestMapping("/createMission")
    public BaseResult createMission(String orderCode){
        return scanGunManager.createMission(orderCode);
    }

    /**
     * 测试消息队列
     */
    @RequestMapping("/testNoticeQue")
    public BaseResult DirectExchangeProducer(@RequestBody List<Integer> missionid){
        return scanGunManager.DirectExchangeProducer(missionid);
    }

}
